package com.siriuscloud.holidayreservation.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.siriuscloud.holidayreservation.domain.Appoint;
import com.siriuscloud.holidayreservation.domain.AppointNumber;
import com.siriuscloud.holidayreservation.domain.Department;
import com.siriuscloud.holidayreservation.domain.vo.AppointVo;
import com.siriuscloud.holidayreservation.mapper.AppointMapper;
import com.siriuscloud.holidayreservation.mapper.AppointNumberMapper;
import com.siriuscloud.holidayreservation.mapper.DepartmentMapper;
import com.siriuscloud.holidayreservation.utils.MyPage;
import com.siriuscloud.holidayreservation.utils.MyPageVo;
import com.siriuscloud.holidayreservation.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AppointService {

    @Resource
    private AppointMapper appointMapper;

    @Resource
    private AppointNumberMapper appointNumberMapper;

    @Resource
    private DepartmentMapper departmentMapper;

    private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    public List<AppointVo> getAll(String applicationNo) {
        return appointMapper.getAllAppoint(applicationNo);
    }

    public IPage<AppointVo> getAllByPage(Page<?> page, AppointVo appointVo) {
       return appointMapper.getAllAppointByPage(page, appointVo);
    }

    public Boolean create(AppointVo appointVo) throws Exception {
        QueryWrapper<Appoint> appointVoQueryWrapper = new QueryWrapper<>();
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        appointVoQueryWrapper
                .eq("application_no", appointVo.getApplicationNo())
                .in("status", list);
        if(appointMapper.selectCount(appointVoQueryWrapper) > 0 ) {
            throw new Exception("您已经预约，请先取消后再预约。");
        }

        Appoint appoint = new Appoint();
        Date date = dateFormat.parse(appointVo.getDate());
        appoint.setApplicationNo(appointVo.getApplicationNo());
        appoint.setApplicationPhone(appointVo.getApplicationPhone());
        appoint.setDate(date.getTime());
        appoint.setDepartmentId(appointVo.getDepartmentId());
        appoint.setMatterId(appointVo.getMatterId());
        appoint.setCode(Utils.getRandomString(6).toUpperCase());
        appoint.setStatus(1);
        appoint.setApplicationName(appointVo.getApplicationName());

        QueryWrapper<AppointNumber> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date", date.getTime());
        queryWrapper.eq("department_id", appointVo.getDepartmentId());
        AppointNumber appointNumber = appointNumberMapper.selectOne(queryWrapper);
        if (null != appointNumber) {
            if (appointNumber.getUseNumber() >= appointNumber.getTotal()) {
                return false;
            }
            appointNumber.setUseNumber(appointNumber.getUseNumber() + 1);
            appointNumberMapper.updateById(appointNumber);
        } else {
            Department department = departmentMapper.selectById(appoint.getDepartmentId());
            appointNumber = new AppointNumber();
            appointNumber.setUseNumber(1);
            appointNumber.setDate(date.getTime());
            appointNumber.setTotal(department.getNumber());
            appointNumber.setDepartmentId(appoint.getDepartmentId());
            appointNumberMapper.insert(appointNumber);
        }
        return appointMapper.insert(appoint) > 0;
    }

    public Boolean update(Appoint appoint) {
        if(appoint.getStatus() == 5 || appoint.getStatus() == 6) {
            Appoint appoint1 = appointMapper.selectById(appoint.getId());
            QueryWrapper<AppointNumber> queryWrapper = new QueryWrapper<>();
            queryWrapper
                    .eq("department_id", appoint1.getDepartmentId())
                    .eq("date", appoint1.getDate());
            AppointNumber appointNumber = appointNumberMapper.selectOne(queryWrapper);
            appointNumber.setUseNumber(appointNumber.getUseNumber() - 1);
            appointNumberMapper.updateById(appointNumber);
        }
        return appointMapper.updateById(appoint) > 0;
    }

    public Boolean delete(String id) {
        return appointMapper.deleteById(id) > 0;
    }

    public AppointVo getById(String id) {
        return appointMapper.getAppointById(id);
    }
}
